import subprocess
import re
import RPi.GPIO as GPIO
from bottle import route, run, template
import time
from userSettings import UserSettings

class Thermostat:
        
        lastTemp = 0.0
        lastHumidity = 0.0
        maxTempDelta = 0.0
        manualHeat = False
        currentlyHeating = False
        
        def __init__(self,maxTempDelta,settings):
                self.currentlyHeating = False
                self.settings = settings
                self.maxTempDelta = maxTempDelta
                # to use Raspberry Pi board pin numbers
                GPIO.setmode(GPIO.BCM)
                GPIO.setup(self.settings.transistorPin, GPIO.OUT)
                GPIO.output(self.settings.transistorPin, GPIO.HIGH)
                

        def getCurrentTemp(self):
                return self.lastTemp
        
        def getCurrentHumidity(self):
                return self.lastHumidity
                
        def isHeatingManually(self):
                return self.manualHeat

        def toggleHeat(self):
                self.manualHeat = not self.manualHeat

        def isHeating(self):
                return self.currentlyHeating

        def setDesiredTemp(self, desiredTemp):
                self.settings.lastDesiredTemp = desiredTemp
                self.settings.persist()

        def getDesiredTemp(self):
                return self.settings.lastDesiredTemp

        # modified from https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/blob/master/Adafruit_DHT_Driver/Adafruit_DHT_googledocs.ex.py
        def update(self):
                # Run the DHT program to get the humidity and temperature readings
                output = subprocess.check_output([self.settings.sensorExe, self.settings.sensorType, self.settings.sensorPin], timeout=4)
                print(output)
                matches = re.search("Temp =\s+([0-9.]+)", output)
                if (not matches):
                        return
                self.lastTemp = float(matches.group(1))
  
                # search for humidity printout
                matches = re.search("Hum =\s+([0-9.]+)", output)
                if (not matches):
                        return
                
                self.lastHumidity = float(matches.group(1))
                self.__applyHeat()
        
        def __applyHeat(self):
                # for now, ust turn it on when at low temp. I suspect this will need to get smarter for max effiency
                if((self.lastTemp < (self.settings.lastDesiredTemp-self.maxTempDelta)) | self.manualHeat):
                        GPIO.output(self.settings.transistorPin, GPIO.LOW)
                        self.currentlyHeating = True
                elif(self.currentlyHeating & (self.lastTemp < (self.settings.lastDesiredTemp+self.maxTempDelta))):
                        GPIO.output(self.settings.transistorPin, GPIO.LOW)
                        self.currentlyHeating = True
                else:
                        GPIO.output(self.settings.transistorPin, GPIO.HIGH)
                        self.currentlyHeating = False

